package com.beta.restapi.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.beta.common.web.ContentType;
import com.beta.dto.product.ProductItemDto;
import com.beta.entity.Users;
import com.beta.restapi.internal.Consts;
import com.beta.restapi.request.product.SearchRequest;
import com.beta.service.IProductItemsService;
import com.beta.vo.product.ProductItemVO;
import com.beta.vo.product.ProductItemPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * Created by wy on 2016/11/23.
 */
@Api(tags = Consts.Resource.PRODUCTITEMS)
@RestController
@RequestMapping(Consts.API_PATH_PREFIX + Consts.Resource.PRODUCTITEMS)
public class ProductItemsController extends BaseController {

    @Autowired
    private IProductItemsService productItemsService;
    @Autowired
    private MapperFacade beanMapper;

    @ApiResponses({@ApiResponse(code = 200, message = "success", response = ProductItemPageVO.class)})
    @GetMapping(value = "", produces = {ContentType.JSON})
    public Object selectProductItems(@RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                                     @RequestParam(value = "category_id", required = false) Integer categoryId) {
        Users currentUser = getCurrentUser();
        Page<ProductItemDto> page = productItemsService.selectProductItems(
                getMybatisPage(), categoryId, currentUser.getCityCode(), currentUser.getId());
        return ok(pageResponse(page));
    }

    @ApiResponses({@ApiResponse(code = 200, message = "success", response = ProductItemVO.class)})
    @GetMapping(value = "/{productItemId}/query", produces = {ContentType.JSON})
    public Object getProductDetail(@PathVariable("productItemId") Integer productItemId,
                                   @RequestHeader(Consts.HEADER_AUTH_KEY) String token) {
        ProductItemDto productItem = productItemsService.getProductItem(productItemId);
        ProductItemVO productItemVO = beanMapper.map(productItem, ProductItemVO.class);
        return ok(productItemVO);

    }

    @PostMapping(value = "search", produces = ContentType.JSON)
    public Object search(@RequestHeader(Consts.HEADER_AUTH_KEY) String token
            , @RequestBody SearchRequest request) {
        Page<ProductItemDto> searchDtoPage = productItemsService.searchByName(
                getMybatisPage(), getCurrentUserId(), request.getKeywords());
        return ok(pageResponse(searchDtoPage));
    }

    @GetMapping(value = "{dealer_user_id}/dealer", produces = ContentType.JSON)
    public Object getDealerProductList(@PathVariable("dealer_user_id") Integer userId) {
        Page<ProductItemDto> productItems = productItemsService.selectDealerProductItems(getMybatisPage(),
                userId, getCurrentUser().getCityCode());
        return ok(pageResponse(productItems));
    }

    private Object pageResponse(Page<ProductItemDto> page) {
        ProductItemPageVO pageVO = new ProductItemPageVO();
        if (Objects.isNull(page) || CollectionUtils.isEmpty(page.getRecords())) {
            return pageVO;
        }

        pageVO.setProductItems(beanMapper.mapAsList(page.getRecords(), ProductItemVO.class));
        fillPageParam(page, pageVO);
        return pageVO;
    }

}
